package com.xtaller.easy.admin.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.xtaller.common.convert.J;
import com.xtaller.common.convert.V;
import com.xtaller.common.convert.W;
import com.xtaller.easy.admin.base.TApi;
import com.xtaller.easy.admin.config.R;
import com.xtaller.easy.admin.model.SysModule;
import com.xtaller.easy.admin.service.impl.SysModuleService;
import com.xtaller.easy.admin.specification.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Taller on 2018/4/23
 */
@Api(tags = "系统模块api")
@RestController
@RequestMapping("/v1/sys")
@CrossOrigin   //跨域服务注解
public class ModuleApi extends TApi {
    @Autowired
    private SysModuleService moduleService;

    private Map<String, String> BaseVerify() {
        Map<String, String> verify = new HashMap<>();
        verify.put("code", "系统模块编码不能为空");
        verify.put("name", "系统模块名称不能为空");
        return verify;
    }

    @GetMapping("/module")
    @ApiOperation(value = "读取系统模块数据列表")
    public Object getModule() {
        List<SysModule> modules = moduleService.query(W.f(
                W.field("id", "name", "code", "remark", "sort"),
                W.order("sort", "asc")
        ));
        return R.ok(data2Grid(modules.size(), modules));
    }

    @PostMapping("/module")
    @ApiOperation(value = "创建系统模块")
    public Object createModule(@RequestBody ModuleCreate object) {
        JSONObject check = V.checkEmpty(BaseVerify(), object);
        if (check.getBoolean("check"))
            return R.error(check.getString("message"));
        SysModule module = J.o2m(object, SysModule.class);
        module.setCreator(getUserIdByToken());
        // 模块唯一性判断
        if (moduleService.existName(module, true))
            return R.error("系统模块名称已经存在请更换");
        // 编码唯一性判断
        if (moduleService.existCode(module, true))
            return R.error("系统模块编码已经存在请更换");
        module = moduleService.createSysModule(module);
        if (module == null)
            return R.error("创建系统模块失败,请稍后再试");
        return R.ok("创建系统模块成功", formatObject(module));
    }

    @PutMapping("/module")
    @ApiOperation(value = "修改系统模块")
    public Object updateModule(@RequestBody ModuleUpdate object) {
        if(object.getId() == null)
            return R.error("数据Id不能为空");
        if(moduleService.selectById(object.getId()) == null)
            return R.error("数据Id异常");
        JSONObject check = V.checkEmpty(BaseVerify(), object);
        if (check.getBoolean("check"))
            return R.error(check.getString("message"));
        SysModule module = J.o2m(object, SysModule.class);
        module.setReviser(getUserIdByToken());
        // 模块唯一性判断
        if (moduleService.existName(module, false))
            return R.error("系统模块名称已经存在请更换");
        // 编码唯一性判断
        if (moduleService.existCode(module, false))
            return R.error("系统模块编码已经存在请更换");
        module = moduleService.updateSysModule(module);
        if (module == null)
            return R.error("修改系统模块失败,请稍后再试");
        return R.ok("修改系统模块成功", formatObject(module));
    }

    @DeleteMapping("/module/{id}")
    @ApiOperation(value = "删除系统模块")
    public Object deleteModule(@PathVariable("id") String id){
        if(moduleService.selectById(id) == null)
            return R.error("数据Id异常");
        boolean check = moduleService.existMenu(id);
        if(check)
            return R.error("该系统模块下有菜单数据，禁止删除！");
        if(moduleService.deleteSysModule(id, getUserIdByToken()))
            return R.ok("系统模块删除成功");
        return R.error("删除失败请稍后再试");
    }

}
